
import {request} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {login} from "../../utils/asyncWx.js"
Page({
  data: {

  },

  onLoad: function (options) {
    
  },
  onShow(){
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx05c6a45f75f2a68f&secret=8b51bc912faf4e4961c2367e1dba2fbc',
      success (res) {
        const token = res.access_token;
        wx.setStorageSync('token', token)
      }
    })
  },

  async handleGetUserInfo(e){
   try {
      // 1获取用户信息
    // 解构赋值 小程序的写法
    const {encryptedData,rawData,iv,signature} = e.detail;
    // 获取小程序登录后的 code
    const {code} = await login();
    const loginParams = {encryptedData,rawData,iv,signature,code};
    console.log('loginParams',loginParams)
    // 3 发送请求获取用户的 token 值
    const res = await request(
      {
        url:"/users/wxlogin",
        data:loginParams,
        method:"POST"
      }
    );
    //由于这个微信账户没有绑定有微信支付权限的帐号,所以获取不了token
    console.log('res',res)

    // 把token存入缓存中,同时跳回上一个页面

    wx.setStorageSync('token', token);
    wx.navigateBack({
      delta: 1,
    });
   } catch (error) {
     console.log('error',error)
   }

  }
  
})